package com.lkbaby.flyweight;

import java.util.HashMap;
import java.util.Map;

public class Student {

	// 缓存
	private static final Map<String,Student> cache = new HashMap<>();

	// 静态工厂方法
	public static Student create(int id , String name){
		String key = id + "-" + name;
		// 查缓存
		Student student = cache.get(key);
		if (student == null){
			// 未找到，创建，并放入缓存中
			System.out.println("create student ..." + key);
			student = new Student(id, name);
			cache.put(key,student);
		}else{
			System.out.println("found in cache ..." + key);
		}
		return student;

	}

	private final int id;
	private final String name;

	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	}
}
